package com.thinksns.sociax.unit;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.ThinksnsActivity;

import java.lang.reflect.Field;

/**
 * @author LeiYan
 * @describe
 * @date 2017/2/5
 * @contact email:907238552@qq.com
 */

public class AppIconBadgeManager {
    private static final String lancherActivityClassName = ThinksnsActivity.class.getName();

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void setBadge(Context context, int badgeCount) {
        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            setXiaoMiBadge(context, badgeCount);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
            setSonyBadge(context, badgeCount);
        } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
            setSamsungBadge(context, badgeCount);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("huawei")) {
            setHuaWeiBadge(context, badgeCount);
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    private static void setHuaWeiBadge(Context context, int count) {
        try {
            Bundle localBundle = new Bundle();//需要存储的数据
            localBundle.putString("package", context.getPackageName());//包名
            localBundle.putString("class", lancherActivityClassName);
            localBundle.putInt("badgenumber", count);//未读信息条数
            context.getContentResolver().call(
                    Uri.parse("content://com.huawei.android.launcher.settings/badge/"),
                    "change badge", null, localBundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setXiaoMiBadge(Context context, int number) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = null;
        boolean isMiUIV6 = true;
        try {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setContentTitle("您有" + number + "未读消息");
            builder.setTicker("您有" + number + "未读消息");
            builder.setAutoCancel(true);
            builder.setSmallIcon(R.drawable.icon);
            builder.setDefaults(Notification.DEFAULT_LIGHTS);
            notification = builder.build();
            Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField("messageCount");
            field.setAccessible(true);
            field.set(miuiNotification, Integer.valueOf(number));// 设置信息数
            field = notification.getClass().getField("extraNotification");
            field.setAccessible(true);
            field.set(notification, miuiNotification);
        } catch (Exception e) {
            e.printStackTrace();
            //miui 6之前的版本
            isMiUIV6 = false;
            Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
            localIntent.putExtra("android.intent.extra.update_application_component_name", lancherActivityClassName);
            localIntent.putExtra("android.intent.extra.update_application_message_text", number);
            context.sendBroadcast(localIntent);
        } finally {
            if (notification != null && isMiUIV6) {
                //miui6以上版本需要使用通知发送
                if (number == 0) {
                    nm.cancel(101010);
                } else {
                    nm.notify(101010, notification);
                }
            }
        }
    }

    public static void setSamsungBadge(Context context, int count) {
        Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        intent.putExtra("badge_count", count);
        intent.putExtra("badge_count_package_name", context.getPackageName());
        intent.putExtra("badge_count_class_name", lancherActivityClassName);
        context.sendBroadcast(intent);
    }


    private static void setSonyBadge(Context context, int number) {
        boolean isShow = true;
        if (0 == number) {
            isShow = false;
        }
        Intent localIntent = new Intent();
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);//是否显示
        localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", lancherActivityClassName);//启动页
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number);//数字
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名
        context.sendBroadcast(localIntent);
    }
}
